

const { ccclass, property } = cc._decorator;

@ccclass
export default class Player extends cc.Component {

    @property(cc.Integer)
    ball_type: number = 0;

    @property(cc.Sprite)
    ball_icon: cc.Sprite = null;


    private _power_scale: number = 1;
    public get power_scale() {
        return this._power_scale;
    }
    public set power_scale(value) {
        this._power_scale = value;
        this.ball_icon.node.setScale(value > 1 ? 1.5 : 1);
    }

    protected onLoad(): void {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this)
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this)
    }

    private isUp = false;
    private isDown = false;
    private isLeft = false;
    private isRight = false;

    onKeyDown(event) {
        console.log('onKeyDown:', event.keyCode)
        let v = 100;
        switch (event.keyCode) {
            case cc.macro.KEY.up: {
                this.isUp = true
                break;
            }
            case cc.macro.KEY.down: {
                this.isDown = true
                break;
            }
            case cc.macro.KEY.left: {
                this.isLeft = true
                break;
            }
            case cc.macro.KEY.right: {
                this.isRight = true
                break;
            }
        }
        // this.setVelocity()
    }

    protected update(dt: number): void {
        this.setVelocity()

    }

    setVelocity() {
        let v = 200;
        let vec = cc.v2(0, 0)
        if (this.isUp) vec.addSelf(cc.v2(0, v))
        if (this.isDown) vec.addSelf(cc.v2(0, -v))
        if (this.isLeft) vec.addSelf(cc.v2(-v, 0))
        if (this.isRight) vec.addSelf(cc.v2(v, 0))
        if (vec.len() == 0) {
            return;
        }
        let old = this.getComponent(cc.RigidBody).linearVelocity;
        vec.normalizeSelf().mulSelf(v)
        // if (Math.abs(old.x) < 1) { old.x = 0 }
        // if (Math.abs(old.y) < 1) { old.y = 0 }
        // if (vec.x == 0 && old.x != 0) {
        //     vec.x = old.x;
        // }
        // if (vec.y == 0 && old.y != 0) {
        //     vec.y = old.y;
        // }
        // console.log('setVelocity:', vec.x, vec.y)
        this.getComponent(cc.RigidBody).linearVelocity = vec;
    }

    onKeyUp(event) {
        let v = 10;
        console.log('onKeyUp:', event.keyCode)
        switch (event.keyCode) {
            case cc.macro.KEY.up: {
                this.isUp = false
                break;
            }
            case cc.macro.KEY.down: {
                this.isDown = false
                break;
            }
            case cc.macro.KEY.left: {
                this.isLeft = false
                break;
            }
            case cc.macro.KEY.right: {
                this.isRight = false
                break;
            }
        }
        let vec = this.getComponent(cc.RigidBody).linearVelocity
        console.log('onKeyUp:', vec.x, vec.y)
    }

    reset() {
        this.node.stopAllActions();
        this.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, 0);
        this.power_scale = 1;
        this.node.active = false;
    }


    fireBall(fire_rotation: number) {
        // const radians = fire_rotation * Math.PI / 180;
        // const ball_speed = GameConst.ins().ball_speed + 100 * GameModel.ins().ball_fire_speed;
        // this.getComponent(cc.RigidBody).linearVelocity = cc.v2(Math.sin(radians) * ball_speed, Math.cos(radians) * ball_speed);
        // this.ball_status = EnumBallStatus.onFire;
    }

    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
        switch (otherCollider.tag) {
            case 1: {
                //底
                if (this.node.active) {
                    this.getComponent(cc.RigidBody).linearVelocity = cc.v2(0, 0);
                }
                break;
            }
        }
    }
}
